package com.isias.system.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.isias.model.system.SysDept;
import com.isias.model.system.SysMenu;
import com.isias.model.system.SysRoleMenu;
import com.isias.system.Util.DeptUtil;
import com.isias.system.Util.MenuUtil;
import com.isias.system.mapper.SysDeptMapper;
import com.isias.system.mapper.SysUserMapper;
import com.isias.system.service.SysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
@Service
@Transactional(rollbackFor = Exception.class)
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> implements SysDeptService {

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public List<SysDept> findNodes() {
        List<SysDept> sysDepts = sysDeptMapper.selectList(null);
        List<SysDept> depts = DeptUtil.FindNodes(sysDepts);

        return depts;
    }

    @Override
    public boolean removeDeptById(String id) {
        int type = sysDeptMapper.deleteById(id);

        return type == 1;
    }

    @Override
    public List<SysDept> findSysDeptByUserId(String userId) {
        //获取所有菜单 status=1
        List<SysDept> list = sysDeptMapper.SelectMenusWithStatus();
        //根据角色id查询角色分配过的菜单列表
        String userDept = sysUserMapper.selectUserDept(userId);
        //分配菜单id与所有菜单相比，如果有相同的，让isSelect值为true
        for (SysDept sysDept : list) {
            if (sysDept.getId().equals(userDept)){
                sysDept.setSelect(true);
            }
        }

        //转换成树形结构为了最终显示
        List<SysDept> resultList = DeptUtil.FindNodes(list);

        return resultList;

    }
}
